package com.yangchoi.lib_base.network.interceptor

import com.yangchoi.lib_base.utils.DataStoreUtil
import okhttp3.Interceptor
import okhttp3.Response

// 从缓存读取cookie添加到请求
class ReadCookieInterceptor : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()
        val builder = request.newBuilder()
        val domain = request.url.host
        //获取domain内的cookie
        if (domain.isNotEmpty()) {
            val sqDomain: String = DataStoreUtil.readStringData(domain, "")
            val cookie: String = if (sqDomain.isNotEmpty()) sqDomain else ""
            if (cookie.isNotEmpty()) {
                builder.addHeader("Cookie", cookie)
            }
        }
        return chain.proceed(builder.build())
    }
}